package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
public class FileController {
    @RequestMapping("/fileUpload")
    public String fileUpload(MultipartFile file, HttpServletRequest request) throws IOException {
        //获取webapp项目目录真实地址
        String path = request.getServletContext().getRealPath("/");
        //设置上传目录地址
        String upPath = "/static/upload/";
        if (file.isEmpty()) {
            //获取文件名 todo文件名加唯一时间戳
            String fileName = file.getOriginalFilename();
            String filePath = path + upPath + fileName;
            file.transferTo(new File(filePath));
            return "uploadSuccess";//   web-INF/pages/uploadSuccess.jsp
        }
        return "uploadFail";
    }
}
